var jfPersonalization = (function($) {

  var that = window.jfPersonalization ? window.jfPersonalization : {};

  that.url = '/jfPersonalization/updatePage/';
  
  that.handlers = {};
  that.addHandler = function(uri, module, component, params) {
    if (!that.handlers[uri]) {
      that.handlers[uri] = [];
    }
    that.handlers[uri].push({module: module, component: component, params: params});
  };

  that.updatePage = function(event, ui) {
    var updateUrl = that.url;
    
    if (!updateUrl) {
      return;
    }
    
    var currentUrl = ui && ui.nextPage ? ui.nextPage.jqmData('url') : document.location.pathname;
    var handlers = that.handlers[currentUrl];
    
    if (!handlers || !handlers.length) {
      return;
    }

    $.ajax({
      url: updateUrl,
      dataType: 'script',
      type: 'POST',
      data: {handlers: handlers}
    });
    delete that.handlers[currentUrl];
  };

  if ($.mobile) {
    $("div:jqmData(role='page')").live('pagehide', that.updatePage);
  }

  $(document).ready(that.updatePage);

  return that;
  
})(jQuery);